/*D2RSXQoY5VHIyDT362LhkEdAhYyPXZf/Av3C6TaPRtZIA70q2AkJhDYLKfcB7qIW*/
import { ajax, cacheTools, toast } from 'nc-lightapp-front';
/**
 * 联查凭证   包含凭证预览   
 * @author xiejhk
 * @param {*} vo 选中的第一行数据
 * @param {*} pk_bill 单据主键
 * @param {*} billType 单据类型
 * @param {*} appcode 小应用编码
 */
export default function linkvouchar(that, props, vo, pk_bill, appcode) {
	let pk_org;
	let pk_org_v;
	let pk_group;
	let pk_billtype;
	let pk_tradetype;
	let billdate;
	let isrefund;
	let defdoc1;
	let hisqueryDatas = [];
	let hisviewDatas = [];
	let hisviewData;
	let isTransOrVerify = false;
	let pk_system = appcode.substring(0, 4) == '2006' ? 'AR' : 'AP';
	//区分卡片和列表数据格式的不同，采用不同方式获取相应的值
	if (appcode == '20060CRU' || appcode == '20060CRU') {
		pk_billtype = 'DA'//应收债权转移
		pk_tradetype = 'DA'
	} else if (appcode == '20060RCA' || appcode == '20060RCB') {
		pk_billtype = 'DAK'//收款并账
		pk_tradetype = 'DAK'
	} else if (appcode == '20080DT' || appcode == '20080DU') {
		pk_billtype = 'DB'//应付债务转移
		pk_tradetype = 'DB'
	} else if (appcode == '20080PCB' || appcode == '20080PCA') {
		pk_billtype = 'DBK'//付款并账
		pk_tradetype = 'DBK'
	} else if (appcode == '20060VQ') {
		pk_billtype = 'D9'//应收核销
		pk_tradetype = 'D9'
	} else if (appcode == '20080VQ') {
		pk_billtype = 'D9AP'//应付核销
		pk_tradetype = 'D9AP'
	}
	if (vo.data) {
		pk_org = vo.data.values.pk_org.value;
		pk_org_v = vo.data.values.pk_org_v.value;
		pk_group = vo.data.values.pk_group.value;
		pk_billtype = vo.data.values.pk_billtype.value;
		pk_tradetype = vo.data.values.pk_tradetype.value;
		billdate = vo.data.values.billdate.value;
		isrefund = vo.data.values.isrefund ? vo.data.values.isrefund.value : null;
		defdoc1 = vo.data.values.pk_busitype ? vo.data.values.pk_busitype.value : null;
	} else if (vo.rows) {
		pk_org = vo.rows[0].values.pk_org.value;
		pk_org_v = vo.rows[0].values.pk_org_v.value;
		pk_group = vo.rows[0].values.pk_group.value;
		pk_billtype = vo.rows[0].values.pk_billtype.value;
		pk_tradetype = vo.rows[0].values.pk_tradetype.value;
		billdate = vo.rows[0].values.billdate.value;
		isrefund = vo.rows[0].values.isrefund ? vo.rows[0].values.isrefund.value : null;
		defdoc1 = vo.rows[0].values.pk_busitype ? vo.rows[0].values.pk_busitype.value : null;
	} else if (vo && vo.length >= 1 && vo[0].data) {
		//债权债务转移,核销联查凭证	
		for (var j = 0; j < vo.length; j++) {
			let hisqueryData =
			{
				pk_group: vo[j].data.values.pk_group.value,
				pk_org: vo[j].data.values.pk_org.value,
				relationID: pk_bill == 'Verify' ? vo[j].data.values.pk_verify.value + "-1" : vo[j].data.values.pk_transfer_h.value,//此处明细反核销以及核销传递的场景不一样-2-1，后续应着重测试
				pk_billtype: pk_billtype,
				busidate: vo[j].data.values.busidate.value,
				pk_tradetype: pk_tradetype,
				defdoc1: vo[j].data.values.pk_busitype ? vo[j].data.values.pk_busitype.value : null
			}
			hisqueryDatas.push(hisqueryData);

			let msgvo = {
				pk_system: pk_system, //系统编码
				relationid: pk_bill == 'Verify' ? vo[j].data.values.pk_verify.value + "-1" : vo[j].data.values.pk_transfer_h.value, //单据主键 //核销凭证预览仅支持汇总预览因为还未生成凭证，不涉及明细反核销以及实现汇兑损溢
				busidate: vo[j].data.values.busidate.value, //单据日期
				pk_org: vo[j].data.values.pk_org.value, //组织主键
				//pk_org_v:pk_org_v,
				pk_group: vo[j].data.values.pk_group.value, //集团主键
				pk_billtype: pk_billtype, //交易类型
				pk_tradetype: pk_tradetype,
				defdoc1: vo[j].data.values.pk_busitype ? vo[j].data.values.pk_busitype.value : null//业务流
			}

			hisviewDatas.push(msgvo);
		}
		hisviewData = {
			messagevo: hisviewDatas, //支持多笔预览
			desbilltype: ['C0'], //目标单据类型,C0为总账凭证,目前只支持总张凭证
			srcbilltype: pk_billtype //业务组交易类型
		};
		isTransOrVerify = true

	}
	if (isrefund && true == isrefund) {
		if (pk_billtype && pk_billtype == 'F1') {
			toast({ color: 'warning', content: that.state.json['public-000239'] });
			return
		} else if (pk_billtype && pk_billtype == 'F3') {
			toast({ color: 'warning', content: that.state.json['public-000240'] });
			return
		}
	}
	//拼接联查数据
	let querydata = [
		{
			pk_group: pk_group,
			pk_org: pk_org,
			pk_org_v: pk_org_v,
			relationID: pk_bill,
			pk_billtype: pk_tradetype,
			defdoc1: defdoc1
		}
	];
	//拼接预览数据
	var viewData = {
		messagevo: [
			{
				pk_system: pk_system, //系统编码
				relationid: pk_bill, //单据主键
				busidate: billdate, //单据日期
				pk_org: pk_org, //组织主键
				pk_org_v: pk_org_v,
				pk_group: pk_group, //集团主键
				pk_billtype: pk_tradetype, //交易类型
				defdoc1: defdoc1//业务流
			}
		], //支持多笔预览
		desbilltype: ['C0'], //目标单据类型,C0为总账凭证,目前只支持总张凭证
		srcbilltype: pk_tradetype //业务组交易类型
	};
	ajax({
		url: '/nccloud/arap/arappub/linkvouchar.do',
		data: isTransOrVerify ? hisqueryDatas : querydata,
		async: false,
		success: (res) => {
			if (res.success) {
				let preview = true;
				let srcCode = res.data.src;
				if ('_LinkVouchar2019' == srcCode) {
					//走联查
					if (res.data.des) {
						//跳转到凭证界面
						if (res.data.pklist) {
							if (res.data.pklist.length == 1) {
								//单笔联查
								props.openTo(res.data.url, {
									status: 'browse',
									appcode: res.data.appcode,
									pagecode: res.data.pagecode,
									id: res.data.pklist[0],
									n: that.state.json['public-000231'], //'联查凭证'
									backflag: 'noback'
								});
								return;
							} else {
								//多笔联查
								cacheTools.set(res.data.cachekey, res.data.pklist);
								props.openTo(res.data.url, {
									status: 'browse',
									appcode: res.data.appcode,
									pagecode: res.data.pagecode,
									scene: appcode + srcCode,
									n: that.state.json['public-000231'] //'联查凭证'
								});
								return;
							}
						}
					} else {
						//跳转到会计平台
						cacheTools.set(appcode + srcCode, res.data.pklist);
					}
				} else if ('_Preview2019' == srcCode) {
					//走预览
					cacheTools.set(appcode + srcCode, isTransOrVerify ? hisviewData : viewData);
					if(isTransOrVerify){
						//核销，债权债务不支持预览凭证
						preview = false;
						toast({
							duration: 3,
							color: 'warning',
							content: that.state.json['public-000254'] /* 国际化处理： 此种单据不支持凭证预览，请检查凭证是否生成！*/
						});	

					}
				}
				if (isTransOrVerify) {
					isTransOrVerify = false;//用后还原
				}
				if (preview) {
					//打开凭证节点
					props.openTo(res.data.url, {
						status: 'browse',
						appcode: res.data.appcode,
						pagecode: res.data.pagecode,
						scene: appcode + srcCode,
						n: that.state.json['public-000232'] // '凭证预览' 凭证使用这个参数,会计平台不用
					});
				}

			}
		}
	});
}

/*D2RSXQoY5VHIyDT362LhkEdAhYyPXZf/Av3C6TaPRtZIA70q2AkJhDYLKfcB7qIW*/